package com.justnow.offer;

import java.util.Arrays;

/**
 * @author justnow
 * Created on 2020-09-10
 * Description
 *
 * 和最初打家劫舍相同的思路，不过这里要求的是有一个环路的问题。
 * 我们直接排除抢第一家和最后一家的情况
 *https://leetcode-cn.com/problems/house-robber-ii/solution/tong-yong-si-lu-tuan-mie-da-jia-jie-she-wen-ti-by-/
 */
public class Solution36 {

    public int rob(int[] nums) {
        if (nums.length == 0) return 0;
        if (nums.length == 1) return nums[0];
        return Math.max(myRob(Arrays.copyOfRange(nums, 0, nums.length - 1)),
                myRob(Arrays.copyOfRange(nums, 1, nums.length)));
    }

    private int myRob(int[] nums) {
        int pre = 0, cur = 0, tmp;
        for (int num : nums) {
            tmp = cur;
            cur = Math.max(pre + num, cur);
            pre = tmp;
        }
        return cur;
    }
}
